//
//  XJStatusCell.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/23.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit
import SnapKit
class XJStatusCell: UITableViewCell {
    //记录底部视图的顶部约束
    var toolBarConstraint:Constraint?
    
    var statusViewModel: XJStatusViewModel? {
        didSet{
            originalView.statusViewModel = statusViewModel
//            let result = statusViewModel?.status?.retweeted_status
//            print(result)
            
            self.toolBarConstraint?.uninstall()
            
            
            
//            print(statusViewModel?.status?.retweeted_status)
            
            
            
            if statusViewModel?.status?.retweeted_status == nil {
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                retweetView.hidden = true
            }else{
                //先赋值
                retweetView.statusViewModel = statusViewModel
                
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                retweetView.hidden = false
            }
            //给底部视图赋值
            toolBarView.statusViewModel1 = statusViewModel
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        
        backgroundColor = RandomColor()
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(contentView)
//            make.height.equalTo(50)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolBarConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //约束contentView
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(toolBarView)

        }
    }
    
    //懒加载控件
    //原创微博
    private lazy var originalView: XJStatusOriginalView = XJStatusOriginalView()
    //转发视图
    private lazy var retweetView: XJStatusRetweetView = XJStatusRetweetView()
    //底部视图
    private lazy var toolBarView: XJStatusToolBarView = XJStatusToolBarView()
    
}
